#include "GentooDescWidget.h"

#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGroupBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QVBoxLayout>

#include <QListWidget>
#include <QScrollArea>


#include <QFile>

GentooDescWidget::GentooDescWidget(QWidget *parent) : QWidget(parent) {
    
    QHBoxLayout *layout = new QHBoxLayout(this);
	
    QVBoxLayout *Leftlayout = new QVBoxLayout();
// left

// 1.1
    QGroupBox *leftBox = new QGroupBox("信息展示");
    QVBoxLayout *leftBoxLayout = new QVBoxLayout(leftBox);
    Leftlayout->addWidget(leftBox);

// 1.2
    // 文件路径
    QLabel *searchKeyFileLabel = new QLabel("use.desc 文件路径");
    QLineEdit *searchKeyFile = new QLineEdit();
    searchKeyFile->setPlaceholderText("例如: /var/db/repos/gentoo/profiles/use.desc");
    QHBoxLayout* searchKeyFileLayout = new QHBoxLayout();
    searchKeyFileLayout->addWidget(searchKeyFileLabel);
    searchKeyFileLayout->addWidget(searchKeyFile);


    // 搜索关键词
    QLabel *searchKeyLabel = new QLabel("关键词");
    QLineEdit *searchKey = new QLineEdit();
    QHBoxLayout* searchKeyLayout = new QHBoxLayout();
    searchKeyLayout->addWidget(searchKeyLabel);
    searchKeyLayout->addWidget(searchKey);

    // 数据显示
    QListWidget *keyListWidget = new QListWidget();


    QVBoxLayout *Rightlayout = new QVBoxLayout();

    // end
    leftBoxLayout->addLayout(searchKeyFileLayout);
    leftBoxLayout->addLayout(searchKeyLayout);
    leftBoxLayout->addWidget(keyListWidget);
    // leftBoxLayout->addStretch();

// right

// 2.1
    QGroupBox *rightBox = new QGroupBox("信息展示");
    QVBoxLayout *rightBoxLayout = new QVBoxLayout(rightBox);
    Rightlayout->addWidget(rightBox);
    
    layout->addLayout(Leftlayout,1);
    // layout->addLayout(Rightlayout,1);

    // resize(1200, 800);
    resize(600, 400);
    


    connect(searchKeyFile, &QLineEdit::textChanged, [=](const QString &file){
        keys.clear();
        keyListWidget->clear();
        if (QFile(file).exists()) {
            if (QFile(file).fileName().endsWith("desc")) {
                QFile desc(file);
                desc.open(QIODevice::ReadOnly);
                for (auto &str : QString::fromLocal8Bit(desc.readAll()).split("\n"))
                {   
                    QStringList words = str.split(" ");
                    if (!str.startsWith("#") && words.size() > 2) {
                        if (words[1] == '-') {
                            keys[words[0]] = str.right(str.length()- words[0].length()+ 2);
                        }
                    }
                }
                desc.close();
            }
        }
        for (auto s : keys.keys()) {
            keyListWidget->addItem(QString("%1 - %2").arg(s).arg(keys[s]));
        }
    });
    connect(searchKey, &QLineEdit::textChanged, [=](const QString &text){
        keyListWidget->clear();
        for (auto s : keys.keys()) {
            bool found = true;
            for (auto &key : text.split(" "))
            {
                if (!s.contains(key, Qt::CaseSensitivity::CaseInsensitive) && !keys[s].contains(key, Qt::CaseSensitivity::CaseInsensitive)) {
                    found = false;
                }
            }
            if (found) {
                keyListWidget->addItem(QString("%1 - %2").arg(s).arg(keys[s]));
            }
        }
    });

}